package pages

import (
	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/form"
	"newclub/services"
)

func GetStudentsTable(ctx *context.Context) table.Table {

	students := table.NewDefaultTable(ctx, table.DefaultConfigWithDriver("mysql"))

	info := students.GetInfo().SetFilterFormLayout(form.LayoutFilter)

	info.AddField("ID", "id", db.Int).
		FieldSortable()
	info.AddField("名字", "name", db.Varchar)
	info.AddField("性别", "gender", db.Varchar)
	info.AddField("手机号", "phone_number", db.Varchar).
		FieldFilterable()
	info.AddField("创建时间", "created_at", db.Timestamp).
		FieldFilterable(types.FilterType{FormType: form.DatetimeRange})

	info.SetTable("students").SetTitle("学生管理").SetDescription("学生管理")

	formList := students.GetForm()
	formList.AddField("ID", "id", db.Int, form.Default).
		FieldDisableWhenCreate().
		FieldHide()
	formList.AddField("名称", "name", db.Varchar, form.Text).FieldMust()
	formList.AddField("性别", "gender", db.Varchar, form.SelectSingle).
		// 单选的选项，text代表显示内容，value代表对应值
		FieldOptions(services.GetGenders()).
		// 设置默认值
		FieldDefault("男").FieldMust()
	formList.AddField("手机号", "phone_number", db.Varchar, form.Text).FieldMust()
	formList.AddField("创建时间", "created_at", db.Timestamp, form.Datetime).
		FieldHide().FieldNowWhenInsert()

	formList.SetTable("students").SetTitle("学生管理").SetDescription("学生管理")

	return students
}
